﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FCTC_Web.Models;
using DAL;
using FCTC_Web.Utilities;
using FCTC_Web.Controllers.Business.BusinessModel;

namespace FCTC_Web.Controllers.Business
{
    public class AppointmentController : Controller
    {
        public ActionResult EstimatorOverview()
        {
            InitialDLL();
            return View(@"~\views\business\appointments\EstimationOverview.cshtml",Banners.Ins);
        }

        public ActionResult AppointForVehicleEstimation()
        {
            InitialDLL();
            return View(@"~\views\business\appointments\appointmentestimation.cshtml");
        }

        [HttpPost]
        public ActionResult AppointForVehicleEstimation(VehicleEstimation ve)
        {
            InitialDLL(ve.Vehicle);
            //ve.Vehicle.FullName = "商标+车型+配置";
            ve.Vehicle.FullName = ve.CarBrand + ve.BrandType + ve.VehicleStyle;
            ModelState.Remove("Vehicle.FullName");
            if(!string.IsNullOrWhiteSpace(ve.CarBrand))
                ModelState.Remove("Vehicle.CarBrand");
            if (!string.IsNullOrWhiteSpace(ve.BrandType))
                ModelState.Remove("Vehicle.BrandType");
            if (!string.IsNullOrWhiteSpace(ve.VehicleStyle))
                ModelState.Remove("Vehicle.VehicleStyle");

            if (ModelState.IsValid)
            {
                CH.Create<VehicleEstimation>(ve);
                return RedirectToAction("Index", "Default");
            }
            return View(@"~\views\business\appointments\appointmentestimation.cshtml",ve);
        }

        void InitialDLL(Vehicle vehicle = null)
        {
            var brands = VehicleXmlHelper.Brands;

            if (vehicle == null)
            {
                ViewBag.CarBrand = new SelectList(brands, null);
                ViewBag.BrandType = new SelectList(new List<string>());
                ViewBag.VehicleStyle = new SelectList(new List<string>());
                return;
            }

            ViewBag.CarBrand = new SelectList(brands, vehicle.CarBrand);
            var selections = VehicleXmlHelper.GetSelectiosn(vehicle.CarBrand);
            ViewBag.BrandType = new SelectList(selections, vehicle.BrandType);
            var types = VehicleXmlHelper.GetvehicleType(vehicle.CarBrand, vehicle.BrandType);
            ViewBag.VehicleStyle = new SelectList(types, vehicle.VehicleStyle);
        }

    }
}
